<template>
  <div class="pageDetail">
    <el-form
      ref="formRef"
      class="form_item_w2"
      :model="pageForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="奖项名称" prop="awardName">
        <el-input v-model="pageForm.awardName" placeholder="请输入奖项名称" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="颁奖单位" prop="awardingUnit">
        <el-input v-model="pageForm.awardingUnit" placeholder="请输入颁奖单位" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="获奖人" prop="winner">
        <el-input v-model="pageForm.winner" placeholder="请输入获奖人" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="所属学院">
        <TheDeptSelect v-model="pageForm.collageId" :modelValue="pageForm.collageId" :deptType="39" placeholder="请选择所属学院"  :disabled="disabled" @update:modelValue="pageForm.collageId = $event"/>
      </el-form-item>
      <el-form-item label="奖项级别" prop="awardLevelId">
        <el-select v-model="pageForm.awardLevelId" placeholder="请选择奖项级别" :disabled="disabled">
          <el-option 
            v-for="(dictItem,index) in award_name_level" 
            :label="dictItem.dictLabel" 
            :value="dictItem.dictCode"
            :key="index" 
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="获奖等级" prop="awardRankId">
        <el-select v-model="pageForm.awardRankId" placeholder="请选择获奖等级" :disabled="disabled">
          <el-option 
            v-for="(dictItem,index) in award_level" 
            :label="dictItem.dictLabel" 
            :value="dictItem.dictCode"
            :key="index" 
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="成果类型" prop="resultTypeId">
        <el-select v-model="pageForm.resultTypeId" placeholder="请选择成果类型" :disabled="disabled">
          <el-option 
            v-for="(dictItem,index) in personal_result_type" 
            :label="dictItem.dictLabel" 
            :value="dictItem.dictCode"
            :key="index" 
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="获奖时间" prop="awardDate">
        <el-date-picker
          v-model="pageForm.awardDate"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          :disabled="disabled">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="成果介绍" prop="resultIntro" class="item_w1">
        <el-input type="textarea" v-model="pageForm.resultIntro" placeholder="请输入成果介绍" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="证书附件" prop="certificateFileId">
        <FileUpload v-model="pageForm.certificateFileId" :disabled="disabled" :fileInfo="pageForm.children"/>
      </el-form-item>
    </el-form>
    <div class="pageFooterBtn">
      <el-button @click="$router.back()">返回</el-button>
      <el-button type="primary" @click="handleSubmit()" v-if="['add','edit'].includes(action)" :loading="loading">提交</el-button>
    </div>
  </div>
</template>
<script>
import { rules } from './data'
import { useDict } from '@/utils/dict/useDict'
import { awardRecordAddApi, awardRecordEditApi, awardRecordByIdApi } from '@/api/favorite/achievement'
import FileUpload from "@/components/FileUpload";
import TheDeptSelect from '@/componentsView/TheDeptSelect.vue';

export default {
  name: 'TeachingPlanDetail',
  components: { FileUpload, TheDeptSelect },
  data() {
    return {
      rules,
      pageForm: {
        collageId: null,
      },
      loading:false,
      disabled:false,
      award_name_level: useDict('award_name_level'),
      award_level: useDict('award_level'),
      personal_result_type: useDict('personal_result_type'),
      awardId: this.$route.query.awardId,
      action: this.$route.query.action
    }
  },

  created() {
    if (this.awardId) this.getCoursePlanById()
  },
  methods: {
    async handleSubmit() {
      const pureNumberRegex = /^[+-]?(\d+(\.\d*)?|\.\d+)$/;
      if (pureNumberRegex.test(this.pageForm.awardName)) {
        this.$message.error('奖项名称不能为纯数字')
        return
      }
      try {
        await this.$refs.formRef.validate()
        this.loading = true
        let res = {}
        if (this.awardRankId) {
          delete this.pageForm.fileInfo
          res = await awardRecordEditApi(this.pageForm)
        }else{
          res = await awardRecordAddApi(this.pageForm)
        }
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.$router.back()
        } else {
          this.$message.error(res.msg || '操作失败')
        }
      } catch (error) {
        if (error !== 'validate') {
          this.$message.error(msg)
        }
      } finally {
        this.loading = false
      }
    },

    getCoursePlanById(){
      if (this.action === 'look') this.disabled = true
      awardRecordByIdApi(this.awardId).then(res => {
        if (res.code === 200) {
          this.pageForm = Object.assign({},this.pageForm, res.data)
        } else {
          this.$message.error(res.msg)
        }
      })
    }
    
  },
}
</script>
<style scoped lang="scss">
::v-deep .el-select {
	width: 100% !important;
}
::v-deep .el-date-editor{
  width: 100% !important;
}
::v-deep .el-input-number{
  width: 100% !important;
}
</style>